﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace 视频设备
{
    public partial class 注册 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Image1.ImageUrl = "../WebForm1.aspx";
        }          

        protected void Button1_Click(object sender, EventArgs e)
        {
            var checkCode = TextBox7.Text.Trim();
            生成验证码.YanZhengResult yanResult = 生成验证码.CheckCodeSame(checkCode);
            switch (yanResult)
            {
                case 生成验证码.YanZhengResult.Fail:
                    Response.Write("<Script Language='JavaScript'>window.alert('校验码错误！请重新输入！');window.close();</script>");
                    break;
                case 生成验证码.YanZhengResult.Success:
                    CheckSerialNum();
                    break;
                case 生成验证码.YanZhengResult.CodeNull:
                    Response.Write("<Script Language='JavaScript'>window.alert('请获取验证码！')</script>");
                    break;
            }


        }

                //注册用户信息
                 void  register ()
                    {
                        using (Model1Container db = new Model1Container())
                        {
                            设备主人 sh = new 设备主人();
                            string str = "nihao.com";
                            sh.序列号 = TextBox1.Text;
                            sh.用户名 = TextBox2.Text;
                            sh.密码 = TextBox3.Text;
                            sh.姓名 = TextBox5.Text;
                            sh.备注 = TextBox6.Text;
                            sh.url = TextBox1.Text + str;
                            sh.问题 = DropDownList1.Text;
                            sh.回答 = TextBox8.Text;
                            try
                            {
                                db.设备主人集.AddObject(sh);
                                db.SaveChanges();
                                Response.Write("<script>alert('注册成功!')</script>");
                            }
                            catch (Exception es)
                            {
                                throw es.InnerException;
                            }
                        }  
                 }


                     //判断序列号
                 void CheckSerialNum()
                 {

                     设备主人 sh = new 设备主人();
                     //序列号
                     var serialnum = TextBox1.Text;
                     var name = TextBox2.Text;
                     设备主人集.AuthResult addResult = 设备主人集.add(serialnum, name);
                     switch (addResult)
                     {
                         case 设备主人集.AuthResult.SerialNumnotNull:
                             Response.Write("<script>alert('抱歉,该序列号已被使用!')</script>");
                             break;
                         case 设备主人集.AuthResult.Register:
                             register();
                             break;
                         case 设备主人集.AuthResult.DoesnotMatch:
                             Response.Write("<script>alert('抱歉,您填写的序列号不存在!')</script>");
                             break;
                         case 设备主人集.AuthResult.Exist:
                             Response.Write("<script>alert('抱歉,该用户名已被注册!')</script>");
                             break;
                     }
                 }

                 protected void Button3_Click(object sender, EventArgs e)
                 {
                     Response.Redirect("WebForm1.aspx");
                 }

                 protected void Button3_Click1(object sender, EventArgs e)
                 {
                     Image1.ImageUrl = "../WebForm1.aspx";
                 }     
    }
}